﻿using FinalPointSet.Model.Asset;
using FinalPointSet.Model.Manager;
using FinalPointSet.Model.Model.Knowledge;
using SharpVectors.Converters;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using FinalPointSet.ViewModel.Page.FieldMapping;
using FinalPointSet.Model.Model;

namespace FinalPointSet.View.Page.FieldMapping
{
    /// <summary>
    /// DataSourceSettingPage.xaml 的交互逻辑
    /// </summary>
    public partial class DataSourceSettingPage : System.Windows.Controls.Page
    {
        public DataSourceSettingPage()
        {
            InitializeComponent();
        }

        private void Page_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                e.Handled = true; // 阻止Tab键的默认行为
            }
        }

        /// <summary>
        /// 点击某行的图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var dependencyObject = (DependencyObject)e.OriginalSource;
            while (dependencyObject != null && dependencyObject is not DataGridCell)
            {
                dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
            }

            if (dependencyObject != null)
            {
                var dataGridCell = (DataGridCell)dependencyObject;
                var dataSource = (DataSource)dataGridCell.DataContext;
                string headerName = StringResourceManager.Instance.Get("FieldMapping_Setting_DataSouce_Logo");
                if (dataGridCell.Column is DataGridTemplateColumn column && headerName.Equals(column.Header.ToString()))
                {
                    DataSourceSettingPageViewModel viewModel = (DataSourceSettingPageViewModel)DataContext;
                    viewModel.DataSource = dataSource;
                    viewModel.IsIconPickerVisible = true;
                }
            }
        }

        /// <summary>
        /// 点击图标选择框的图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SvgViewbox_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var viewModel = DataContext as DataSourceSettingPageViewModel;

            if (viewModel?.DataSource != null && ((SvgViewbox)sender).DataContext is TextPair item)
            {
                viewModel.DataSource.LogoPath = item.Value;
                viewModel.IsIconPickerVisible = false;
                viewModel.DataSources = new(viewModel.DataSources);
            }
        }
    }
}
